package com.deuwise.system.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import io.swagger.annotations.*;
import io.swagger.annotations.ApiImplicitParams;
import com.deuwise.common.utils.R;
import com.deuwise.common.utils.Result;
import com.baomidou.mybatisplus.plugins.Page;
import com.deuwise.system.entity.SysLayerFieldOptional;
import com.deuwise.system.service.SysLayerFieldOptionalService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 *@author hezhenhao
 *@date 2020-05-27 13:26:17
 */
@Api(tags = "图层字段可选项配置模块", description = "图层字段可选项")
@RequestMapping("system/sysLayerFieldOptional")
@RestController()
public class SysLayerFieldOptionalController extends AbstractController{
    @Autowired
    private SysLayerFieldOptionalService sysLayerFieldOptionalService;

    @ApiOperation(value = "获取list", notes = "")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fieldId", value = "图层字段ID", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "currentPage", value = "开始页数", defaultValue = "1", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页多少条", defaultValue = "15", dataType = "int", paramType = "query")})
    public Result<Page<SysLayerFieldOptional>> list(String fieldId){
        Wrapper<SysLayerFieldOptional> wrapper = new EntityWrapper<SysLayerFieldOptional>();
        if(StringUtils.isNotBlank(fieldId)){
            wrapper.eq("field_id", fieldId);
        }
        Page<SysLayerFieldOptional> listPage = sysLayerFieldOptionalService.selectPage(getPage(SysLayerFieldOptional.class),wrapper);
        return Result.ok(listPage);
    }

    @ApiOperation(value = "新增数据")
    @RequestMapping(value = "/insertSysLayerFieldOptional", method = RequestMethod.POST)
    public R insertSysLayerFieldOptional(@RequestBody SysLayerFieldOptional sysLayerFieldOptional){
        boolean isSuccess = false;
        if(null == sysLayerFieldOptional){
            return R.error(200,"新增数据失败，请稍后再试!");
        }
        isSuccess = sysLayerFieldOptionalService.insert(sysLayerFieldOptional);
        if (isSuccess) {
            return R.ok();
        } else {
            return R.error(200,"新增数据失败，请稍后再试!");
        }
    }

    @ApiOperation(value = "编辑数据")
    @RequestMapping(value = "/updateSysLayerFieldOptional", method = RequestMethod.POST)
    public R updateSysLayerFieldOptional(@RequestBody SysLayerFieldOptional sysLayerFieldOptional){
        boolean isSuccess = false;
        if(null == sysLayerFieldOptional){
            return R.error(200,"数据编辑失败，请稍后再试!");
        }
        isSuccess = sysLayerFieldOptionalService.updateById(sysLayerFieldOptional);
        if (isSuccess) {
            return R.ok();
        } else {
            return R.error(200,"数据编辑失败，请稍后再试!");
        }
    }

    @ApiOperation("删除字典")
    @DeleteMapping("/{id}")
    public R delete(@PathVariable("id") String id){

        sysLayerFieldOptionalService.deleteById(id);

        return R.ok();
    }

    @ApiOperation(value = "批量删除记录")
    @RequestMapping(value = "/deleteSelect", method = RequestMethod.POST)
    public  R deleteSelect(@ApiParam(value = "ids集合",name = "ids",required = true) @RequestParam String[] ids){
        try {
            if (ids != null && ids.length > 0) {
                sysLayerFieldOptionalService.deleteBatchIds(Arrays.asList(ids));
            }
        }catch (Exception ex){
            return  R.error("删除失败");
        }
        return  R.ok();
    }

}